package com.yc.javaee.tomcat.impl;

import com.yc.javaee.tomcat.HttpSession;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HttpSessionImpl implements HttpSession {

    private long creationTime = new Date().getTime();
    // 属性集合
    private Map<String,Object> attributeMap = new HashMap<>();
    // 64位16进制数字字符串
    private String id = UUID.randomUUID()
            .toString().replaceAll("-","");

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public long getLastAccessedTime() {
        return 0;
    }

    @Override
    public int getMaxInactiveInterval() {
        // 默认有效30分钟
        return 60 * 30;
    }

    @Override
    public void invalidate() {
        // 自动销毁会话
        id = "";
        attributeMap.clear(); // 清空属性
    }

    @Override
    public void setAttribute(String name, Object value) {
        attributeMap.put(name,value);
    }

    @Override
    public Object getAttribute(String name) {
        return attributeMap.get(name);
    }

    @Override
    public void removeAttribute(String name) {
        attributeMap.remove(name);
    }
}
